
local CommonDefine = require("common.CommonDefine")
local Util = require("common.Util")
local Log = require("common.Log")
local Lang = require("common.Lang")
local ExcelMapConf = require("excel.Map")
local Map = require("core.Map")
local Msg = require("core.Msg")
local Timer = require("core.Timer")
local Obj = require("core.Obj")
local ObjHuman = require("core.ObjHuman")
local ObjMonster = require("core.ObjMonster")
local RoleFFI = require("role.RoleFFI")
local Status = require("scene.Status")
local SkillNotify = require("skill.Notify")
local Buffer   = require("buffer.Buffer")
local Cmds     = require("buffer.Cmds")
local BagLogic = require("bag.BagLogic")
local StoreLogic = require("store.StoreLogic")
local JumpInfo = require("config.JumpInfo")
local EquipLogic = require("bag.EquipLogic")
local SceneEvent = require("core.SceneEvent")
local TaskLogic  = require("task.TaskLogic")
local TaskDefine = require("task.Define")
local RoleUIControl = require("role.RoleUIControl")
local Copy = require("copy.CopyL")
local BeginStory = require("scene.BeginStory")
local Broadcast  = require("broadcast.Broadcast")
local BaseAttrInitValue = require("excel.Role").baseAttrInitValue[1]
local RoleDefine = require("role.RoleDefine")
local sbConfig = require("excel.Shenbing").shenbing
local HorseLogic = require("horse.HorseLogic")
local FcmLogic = require("fcm.FcmLogic")
local map_mgr = _G.map_mgr
local RoleRevive = require("role.RoleRevive")
local FuLiLoginLogic = require("fuli.LoginLogic")
local CowryBagLogic = require("fuli.CowryBagLogic")
local RoleCombatMode = require("role.RoleCombatMode")
local FuliHandler = require("fuli.HandlerL")
local SmeltLogic = require("bag.SmeltLogic")
local RefineHandle = require("refine.HandlerL")
local PetLogic = require("pet.PetLogic")
local BufferNotify = require("buffer.Notify")
local ExcelNpc = require("excel.Npc")
local CopyL = require("copy.CopyL")
local CopyDefine = require("copy.Define")
local YellowLogic = require("yellow.YellowLogic")
local ItemDefine = require("bag.ItemDefine")
local MonthCardLogic = require("vip.MonthCardLogic")
local ArmyRiotLogicL = require("activity.armyRiot.ArmyRiotLogicL")
local WildBossLogicL = require("wildboss.WildBossLogicL")
local QhLogic = require("refine.QhLogic")
local BaowuLogic = require("baowu.BaowuLogic")
local ExcelMonsterConf = require("excel.Monster")
local CopyHandler = require("copy.HandlerL")
local Config = require("Config")
local OperateActLogicL = require("activity.operate.OperateActLogicL")
local FindResLogic = require("findRes.FindResLogic")
local FamilyLogicFL = require("family.FamilyLogicFL")
local ComplexActivityPlay = require("activity.complex.ActivityPlay")
local ComplexCopyFast = require("activity.complex.CopyFast")
local VipLogic = require("vip.VipLogic")
local BlueLogic = require("yellow.BlueLogic")
local ChongzhiRebate = require("activity.operate.ChongzhiRebate")
local ChongzhiDateRebate = require("activity.date.ChongzhiRebate")
local QiliaoLogic = require("qiliao.QiliaoLogic")
local DateActLogicL = require("activity.date.DateActLogicL")
local TGPLogic = require("yellow.TGPLogic")
local TitleLogic = require("title.TitleLogic")
local OfficialLogic = require("official.OfficialLogic")

function sendHumanInfo(human)
    local mm = Msg.gc.GC_ZZ_HUMAN_INFO
    mm.server_time = os.time()
    mm.id = human.id
    mm.uuid = human._id
    mm.name = human.name
    mm.sex = human.db.sex
    mm.fcm = FcmLogic.isFcmRegistered(human) and 1 or 0
    mm.head = human.db.head
    mm.familyUuid = human.familyUuid or ""
    mm.familyName = human.familyName or ""
    Msg.send(mm, human.fd)
end

function WL_ASK_LOGIN(human, msg)
    local fd = msg.fd
    local account = msg.account
    local human_old = ObjHuman.onlineAccount[account]  
    if human_old then
        if human_old.fd then
            assert()
        end
        -- 正常退出保留10s未到,提前干掉之
        ObjHuman.doDisconnect(human_old, CommonDefine.DISCONNECT_DUPLICATE)
    end
    local db = RoleFFI.find_ffi_by_fd(fd)
    local human = ObjHuman.create(fd, account,db, msg.id, msg.uid, true)  
    sendHumanInfo(human) 

    FcmLogic.onLogined(human)
    --所有跳转点信息
    JumpInfo.sendJumpInfos(human)

    human.isLogin = true
    
    if Config.IS_MIDDLE and msg.middleCopyID ~= 0 then
        if CopyHandler.copyEnter(human, msg.middleCopyID) then
            return
        end
        print(".....middle  can't longin")
        ObjHuman.destroy(human)
        local msg = Msg.lw.LW_ASK_CHANGE_SCENE
        msg.map_id = human.db.map_id
        msg.x = human.db.x
        msg.y = human.db.y
        msg.middle = 1
        Msg.send2world(msg, human._id)
        return
    end
    if not BeginStory.checkLogin(human) then
        ObjHuman.enterMap(human,human.db.map_id,human.db.x, human.db.y)
    end
end
--登陆函数
function onUserLogin(human)  
    local today = os.date("%Y%m%d", os.time())
    local lastDay = os.date("%Y%m%d", human.db.lastLogoutTime)
    if lastDay ~= today or human.db.lastLogoutTime == 0 then
        ObjHuman.humanLoginCrossDay(human)
        if human.db.lastLogoutTime == 0 then
            human.db.lastLogoutTime = os.time()
        end
    end
--    ObjHuman.sendAllAttr(human)
    print("use loginc ok account: ", human.account, " uuid:", human._id, " xy=", human.db.x, human.db.y)  
    --skill list
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human, true)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
    HorseLogic.sendGC_HORSE_HEIGHT(human)
    --bag cap
    BagLogic.sendCap(human) 

    --bag list
    BagLogic.sendBagList(human, true)

    EquipLogic.CG_EQUIP_QUERY(human,human._id,EquipLogic.EQUIP_QUERY)

    --store cap
    StoreLogic.sendStoreCap(human)

    --store list
    StoreLogic.sendStoreList(human, true)

    --Buffer list
    BufferNotify.sendBufferList(human)

    --坐骑状态
    HorseLogic.sendHorseStatus(human)
    HorseLogic.send_GC_HORSE_STAR_UP_ITEM_INFO(human)
    HorseLogic.sendXXList(human)

    --人物设置
    ObjHuman.sendHumanSet(human)
    ObjHuman.sendAutoUseDrugPer(human)

    -- 右下角图标
    RoleUIControl.sendMenuRightControl(human)

    --福利
    FuliHandler.sendFuliList(human)

    Log.write(Log.LOGID_OSS_LOGIN, human._id, human.account, human.name, human.db.lv, human.ip, human.pf)


    TaskLogic.sendTaskList(human, TaskDefine.TASK_LIST_FLUSH_LOGIN)
    -- 状态信息
    Status.sendViewStatus(human)  

    --福利大厅
    CowryBagLogic.sendCowryBagList(human)
    FuLiLoginLogic.sendLoginSbInfo(human)
    FuliHandler.checkShowTips(human)

--    --元宝熔炼
--    SmeltLogic.sendSmeltTimes(human)
--    SmeltLogic.sendIsPut(human)
--    SmeltLogic.sendRemainHelpTimes(human)

    --锻造  
    RefineHandle.CG_OPEN_REFINE_WINDOW(human)

    --神将
    PetLogic.sendPetList(human)
    PetLogic.addAllRA(human)
    PetLogic.sendAtticInfo(human)

    --黄钻
    YellowLogic.addBuffer(human)
    
    TaskLogic.onUserMainTaskFix(human)
    ArmyRiotLogicL.sendGC_ARMY_RIOT_MONSTER_ID(human)
    --至尊月卡
    MonthCardLogic.checkAddMonthCardBag(human)
    --vip
    VipLogic.checkSendVipMail(human)
    --强化属性
    QhLogic.sendAllAttr(human)
    
    --宝物
    BaowuLogic.sendBaowuList(human)
    BaowuLogic.sendNeedItem(human)
    
    --运营活动
    OperateActLogicL.userLogin(human)
    --和服活动
    ComplexActivityPlay.checkAllCanGet(human)
    ComplexCopyFast.checkCanGet(human)
    --定时活动
    DateActLogicL.userLogin(human)
    
    --资源找回
    FindResLogic.sendActFind(human)
    FindResLogic.sendCopyFind(human)
    FindResLogic.sendExpFind(human)
    
    if Config.IS_MIDDLE then
      ChongzhiRebate.checkCanGet(human)
      ChongzhiDateRebate.checkCanGet(human)
    end
    
    --军团战
    FamilyLogicFL.onUserLogin(human)   
    
    --气疗系统
    QiliaoLogic.sendYaoshuList(human)
    
    TGPLogic.addBuffer(human)
    
    --称号
    TitleLogic.onLogin(human)
    
    --军衔
    OfficialLogic.checkOneCondition(human, "familyLv")  
    OfficialLogic.sendCanUpgrade(human)  

    ObjHuman.doCalc(human)
    ObjHuman.sendAllAttr(human)
    ObjHuman.sendSkillItemList(human)
    local msg = Msg.lw.LW_ASK_LOGIN_OK
    msg.sceneid = human.scene_id
    Msg.send2world(msg, human._id)  

end
function WL_ASK_CHANGE_SCENE(uuid, msg)
    local account = msg.account
    local fd      = msg.fd
    local db = RoleFFI.find_ffi_by_fd(fd)
    if msg.account ~= RoleFFI.ffi_string(db.account) then
        assert()
    end
    local human_old = ObjHuman.onlineAccount[account]
    if human_old then
        assert()
    end
    local human = ObjHuman.create(fd, account,db, msg.id, msg.uid, msg.login == 1)

    local x,y = map_mgr.get_fix_xy(msg.map_id, msg.x, msg.y)

    human.isLogin = msg.login == 1 and true or nil
    ObjHuman.enterMap(human,msg.map_id, x, y)

    print("WL_ASK_CHANGE_SCENE ok account:", account, " uuid:", human._id, " x: "..x.." y:"..y)
end

function WL_AA_DISCONNECT(human, msg)
    print("WL_AA_DISCONNECT:"..human.name)
    ObjHuman.doDisconnect(human, msg.code, CommonDefine.DISCONNECT_MSG[msg.code])
end

function CG_ENTER_SCENE_OK(human, msg)
    local map_id = Map.scene_id_to_map_id[human.scene_id]
    if map_id ~= msg.map_id then 
--        assert(nil, "cgentersceneok mapid error " .. map_id .. " " .. msg.map_id) 
        return
    end
    -- 先切状态再向周围广播自己的状态
    if human.status ~= Status.HUMAN_STATUS_TALISMAN then
        if human.status == Status.HUMAN_STATUS_NORMAL then
            Status.sendViewStatus(human)
        else
            Status.doHumanControlStatusChange(human,Status.HUMAN_STATUS_NORMAL)
        end
    else
        Status.sendViewStatus(human)
    end

    Obj.sendArea(ObjHuman.getObjAdd(human), human.scene_id, human.x, human.y, human.fd)
    local list = Msg.list
    Obj.getAreaObjs(list, human.scene_id, human.x, human.y, human)
    for k = 1, list[0] do
        Msg.send(Obj.getObjAdd(list[k]), human.fd)
    end
    human.hide = nil
    if human.fd then Obj.scenes_fds[human.scene_id][human.fd] = human end


    local mm = Map.list_gc_add_obj[human.db.map_id]
    if mm then
        Msg.send(mm, human.fd)
    end

    local list = SceneEvent.ban[human.scene_id]
    if list then
        local mm = Msg.gc.GC_BAN_ADD
        for _, area_id in ipairs(list) do
            local list_rect = Map.getRects(map_id, area_id)
            mm.rect[0] = 0
            for i = 1, #list_rect do
                local right = list_rect[i]
                mm.rect[0] = mm.rect[0] + 1
                local left = mm.rect[mm.rect[0]]
                left.x1 = right[1]
                left.y1 = right[2]
                left.x2 = right[3]
                left.y2 = right[4]
            end
            Msg.send(mm, human.fd)
        end
    end
    RoleUIControl.sendFollowerNPCControl(human)
    RoleUIControl.sendRightPanelControl(human)
    RoleUIControl.sendTipTopControl(human)



    local tb = SceneEvent.piaohua[human.scene_id]
    if tb then
        local list_rect = Map.getRects(map_id, tb[1])
        local mm = Msg.gc.GC_PIAO_HUA
        mm.cmd = tb[2]
        mm.effect_id = tb[3]
        if tb[1] == -1 then
            mm.x = 0
            mm.y = 0
            mm.half_w = 0
            mm.half_h = 0
            Msg.send(mm, human.fd)
        else
            for _, v in ipairs(list_rect) do
                mm.x = (v[1] + v[3]) / 2
                mm.y = (v[2] + v[4]) / 2
                mm.half_w = (v[3] - v[1]) / 2
                mm.half_h = (v[4] - v[2]) / 2
                Msg.send(mm, human.fd)
            end
        end
    end
    Copy.onEnter(human, human.scene_id)
    Msg.send(Msg.gc.GC_ENTER_SCENE_OK, human.fd)
    
    BufferNotify.sendBufferList(human)  --这里重发一遍buffer list
    RoleCombatMode.sendCombatMode(human, true)

    print("CG_ENTER_SCENE_OK ok111 account:", human.account)

    --判断玩家是否死亡状态
    local nReviveLeftTime = RoleRevive.getReviveLeftTime(human)
    if human.db.hp < 1 and nReviveLeftTime <= 0 then
        RoleRevive.reviveGoHome(human)
    elseif human.db.hp < 1 and nReviveLeftTime > 0 then
        RoleRevive.sendHumanDie(human)
        Status.doHumanControlStatusChange(human, Status.HUMAN_STATUS_NORMAL)
    end

    if human.db.mp < 1 then
        human.db.mp = 0x7fffffff
    end
    BeginStory.checkEnterSceneOk(human)


    if human.scene_id then
        local msg = Msg.lw.LW_ENTER_SCENE_OK
        msg.sceneid = human.scene_id
        Msg.send2world(msg, human._id)  
    end
    WildBossLogicL.onSendFirstHitData(human)
    ObjMonster.sendMonsterCorpseList(human.scene_id, human)
end

function CG_ASK_CHANGE_SCENE(human, msg)
    if human.db.hp < 1 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_FLY_FAIL_DIE)
        return
    end
    local mapID = msg.map_id
    if not ExcelMapConf.scene[mapID] then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ITEM_MAP_ID_INVALID) 
        return 
    end

    if msg.check == 0 then -- 这里是剧情里面切换场景的情况
        ObjHuman.enterMap(human, mapID, ExcelMapConf.scene[mapID].x, ExcelMapConf.scene[mapID].y)
        return
    end
    local tempSec = ObjHuman.checkFightSec(human,10)
    if tempSec>0  then
        Broadcast.sendBroadcast(human, Lang.CENTER, string.format(Lang.TASK_FLY_FAIL_SKILL,tempSec))
        return
    end	
    if human.db.lv < 30 then --
        Broadcast.sendBroadcast(human,Lang.MOUSE, string.format(Lang.ITEM_FLY_LV_NOT_ENOUGH,30))
        return
    end
    -- 进入等级限制判断	
    if human.db.lv < ExcelMapConf.scene[mapID].lvLimit then
        Broadcast.sendBroadcast(human,Lang.MOUSE, string.format(Lang.ITEM_FLY_LV_NOT_ENOUGH,ExcelMapConf.scene[mapID].lvLimit))
        return
    end
    local mapNowID = Map.scene_id_to_map_id[human.scene_id]
    if ExcelMapConf.scene[mapNowID].can_task_fly == 0 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ITEM_NO_FLY)
        return
    end			
    if not MonthCardLogic.isHaveSpec(human) then
      if BagLogic.getItemCount(human, ItemDefine.ITEM_ID_FLY) < 1 then
          Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_FLY_FAIL_ITEM)
          return
      end
      BagLogic.delItemsById(human, ItemDefine.ITEM_ID_FLY, 1,"task_fly")
    end
    ObjHuman.enterMap(human, mapID, ExcelMapConf.scene[mapID].x, ExcelMapConf.scene[mapID].y)
end

function CG_MOVE(human, m)	
    if m.points[0] % 2 == 1 then
        assert()
    end

    local len = m.points[0]
    if len < 2 then
        return
    end

    local t = m.points
    for i = 1, len do
        if t[i] < 0 then
            --assert(nil, human.db.name)
            return
        end
    end

    if human.hide then
        return
    end

    if human.scene_id == nil then
        return
    end

    if human.db.hp < 1 then
        return --Obj.sendGCObjDie(human)
    end

    if not Status.canMove(human) then 
        return 
    end

    --	print("CG_MOVE mx:", m.x, " my:", m.y, " x:", human.x, " y:", human.y, " target:", m.points[1], m.points[2])

    Status.onMove(human)

    if m.points[len - 1] == m.x and m.points[len] == m.y then
        --		print("same_point1")
        return
    end
    if 0 < human.path_len and human.path[1] == m.points[len - 1] and human.path[2] == m.points[len] then
        --		print("same_point2")
        return
    end

    human.path_time = Timer.now
    human.cg_move_time = Timer.now
    human.attacker_id = nil
    human.attacker_uid = nil
    if Obj.checkClientPos(human, m.x, m.y) == true then
        human.path_client_x = m.x
        human.path_client_y = m.y
    else
        --		print("fck == client go ,server not go task can't accept mx:", m.x, " my:", m.y, " x:", human.x, " y:", human.y)
        human.path_client_x = nil
        human.path_client_y = nil
    end	

    local mm = Msg.gc.GC_MOVE
    mm.obj_id = human.id
    mm.x = human.x
    mm.y = human.y
    if human.path_client_x then mm.x = human.path_client_x end
    if human.path_client_y then mm.y = human.path_client_y end
    mm.points = m.points
    Obj.sendArea(mm, human.scene_id, human.x, human.y, human.fd)

    human.path_len = m.points[0] / 2 
    local path_len = human.path_len
    human.path = human.path or {}
    for i = 1, path_len do
        local j = path_len + 1 - i
        human.path[2 * i - 1] = m.points[2 * j - 1]
        human.path[2 * i] = m.points[2 * j]
        -- print("i:", i, " x:", human.path[2 * i - 1], " y:", human.path[2 * i])
    end
end

function CG_HEART_BEAT(human)
    local mm = Msg.gc.GC_HEART_BEAT
    mm.time = Timer.now
    Msg.send(mm,human.fd)
    --  print("CG_HEART_BEAT")
end

function CG_SCRIPT_STOP(human, m)
    local copyid =  Copy.isCopyBySceneID(human.scene_id)
    if copyid then
        local copyID = Copy.sceneID2CopyID[human.scene_id]
        local roomID = Copy.sceneID2Room[human.scene_id]
        if copyID and roomID then
            Copy.sendCopy(Msg.gc.GC_SCRIPT_STOP, copyID, roomID)
            Copy.copyScriptStop(copyID, roomID, m.script_id)
        end
    end
end

-- 跳跃机关
function CG_NPC_JUMP(human, msg)
    local npcID = msg.npcID
    local npcConfig = ExcelNpc.npc[npcID]
    if npcConfig == nil then return end
    if #npcConfig.jump ~= 2 then return end

    Status.onJump(human)
    Obj.jump(human, npcConfig.jump[1], npcConfig.jump[2], Msg.gc.GC_NPC_JUMP)
    local pet = Obj.getObj(human.fight_pet_id, human.fight_pet_uid)
    if pet then
        Obj.jump(pet, npcConfig.jump[1], npcConfig.jump[2], Msg.gc.GC_JUMP)
    end
end

-- 挂机点
local tempList = tempList or {}
function CG_HANG_POINT(human, msg)
    local mapID = Map.scene_id_to_map_id[human.scene_id]
    local cf = ExcelMapConf.scene[mapID]

    local mm = Msg.gc.GC_HANG_POINT
    mm.x = human.x		
    mm.y = human.y
    mm.radius = 800
    if #cf.hangPoints>0 then
        mm.type = 2		

        local list = tempList
        local len = 0
        -- 最近点
        for i,v in ipairs(cf.hangPoints) do
            local distLen = Util.dist(human.x,human.y,v[1],v[2])
            local ans = 0
            for j = len, 1, -1 do
                local point = cf.hangPoints[list[j]]
                if Util.dist(human.x,human.y,point[1],point[2]) <= distLen then
                    ans = j
                    break
                else
                    list[j + 1] = list[j]
                end
            end
            len = len + 1
            list[ans + 1] = i
        end

        -- 最近点并且有怪物
        for j=1, len do
            local value = list[j]
            local point = cf.hangPoints[value]
            for _, obj in pairs(Obj.scenes[human.scene_id]) do
                if obj.obj_type == Obj.TYPE_MONSTER and Obj.getHp(obj) > 0 and  ObjMonster.getValue(obj, "camps") == "enemy" and ObjMonster.getValueByMonsterType(obj, "can_select") == 1  then
                    local dx = obj.x - point[1]
                    local dy = obj.y - point[2]
                    if -Obj.GRID_WIDTH < dx and dx < Obj.GRID_WIDTH and -Obj.GRID_HEIGHT < dy and dy < Obj.GRID_HEIGHT then
                        mm.type = 1	
                        mm.x = point[1]
                        mm.y = point[2]
                        mm.radius = point[3] or 800
                        mm.index = value
                        Msg.send(mm, human.fd)
                        return
                    end
                end
            end
        end


        -- 地图都没有任何怪物 通知往下一个点
        local index = msg.index+1>list[1] and (msg.index+1) or list[1]
        local point = cf.hangPoints[index]
        if point then
            mm.index = index
        else
            point =  cf.hangPoints[#cf.hangPoints]
            mm.index = #cf.hangPoints
        end 

        mm.x = point[1]
        mm.y = point[2]
        mm.radius = point[3] or 800
        mm.type = 1
    else
        mm.index = 0
        mm.type = 0
    end	
    --type 0:无挂机点 1:有挂机点 2:有挂机点，暂时无怪,客户端等待(原地挂机)
    Msg.send(mm, human.fd)
end

function sendSceneMove(human)
    local map_id = Map.scene_id_to_map_id[human.scene_id]
    local cf = ExcelMapConf.scene[map_id]
    if cf.sceneMove[1] and cf.sceneMove[2] then
        local mm = Msg.gc.GC_SCENE_MOVE
        mm.x = cf.sceneMove[1]
        mm.y = cf.sceneMove[2]
        mm.rotation = cf.sceneMove[3]
        Msg.send(mm,human.fd)
    end
end

function CG_ENTER_PVP_WARNING_CONFIRM(human, msg)
    local newMapID = msg.mapID 
    local areaID = msg.areaID
    local nowMapID = Map.scene_id_to_map_id[human.scene_id]

    local sceneConfig = ExcelMapConf[nowMapID]
    for index, sceneEvent in pairs(sceneConfig) do
        if sceneEvent.action_cmd1 == "jump" then
            if sceneEvent.action_arg1[2] == newMapID and sceneEvent.action_arg1[1] == areaID then
                if msg.isYes == 1 and human.db.lv >= ExcelMapConf.scene[newMapID].lvLimit then
                    ObjHuman.enterMap(human, sceneEvent.action_arg1[2], sceneEvent.action_arg1[3], sceneEvent.action_arg1[4])
                else
                    -- 如果还在传送门区域内 那么弹出这个区域
                    if Map.is_in_area_id(nowMapID, human.x, human.y, areaID) then
                        local getawayX,getawayY = Util.getAnglePoint(human.dir, human.x, human.y, 180, 150)
                        Obj.jump(human, getawayX, getawayY, Msg.gc.GC_JUMP)
                    end
                end
                return
            end
        end
    end
end

function CG_CLIENT_INFO(human, m)
    Log.write(Log.LOGID_OSS_CLIENT_INFO, human._id,
    m.cpuArchitecture,
    m.isDebugger,
    m.language,
    m.os,
    m.playerType,
    m.screenDPI,
    m.screenResolutionX,
    m.screenResolutionY,
    m.touchscreenType,
    m.version,
    m.font
    )	
end


function CG_OBJ_INTERACTION(human, m)
    local t = Obj.objs[m.obj_id]
    if MonsterSpCg.isMonsterSpCg(t) then
        return MonsterSpCg.doMonsterSpCg(human, t)
    end
end

function sendDoubleKillTips(human, cnt)
    if not cnt then
        local nowTime = os.time()
        if human.doubleKillLastTime + 10 <= nowTime then
            human.doubleKillCnt = 1
        else
            human.doubleKillCnt = human.doubleKillCnt + 1
        end
        human.doubleKillLastTime = nowTime
        cnt = human.doubleKillCnt
    end
    local msgKill = Msg.gc.GC_DOUBLE_KILL_TIPS
    msgKill.cnt = cnt 
    Msg.send(msgKill,human.fd)
end

function CG_CLIENT_ERR_LOAD(human, m)
    Log.write(Log.LOGID_OSS_CLIENT_ERR_LOAD, human._id, human.account, human.name, 0, m.url)
end

function CG_CLIENT_ERR_DECODE(human, m)
    Log.write(Log.LOGID_OSS_CLIENT_ERR_LOAD, human._id, human.account, human.name, 1, m.url)
end

function CG_CLIENT_ERR_MEM_MAP(human, m)
    Log.write(Log.LOGID_OSS_CLIENT_ERR_MEM_MAP, human._id, human.account, human.name, m.map_id, m.privateMemory, m.totalMemory, m.freeMemory)
end

function CG_RUSH_END(human, m)
    if human.rushx == nil or human.rushy == nil then 
        print("CG_RUSH_END err human has no rushx rushy")
        return
    end

    local dist = (m.x - human.rushx) * (m.x - human.rushx) + (m.y - human.rushy) * (m.y - human.rushy)
    if dist > 62500 then
        print("CG_RUSH_END err clientxy too far mx:", m.x, " my:", m.y, " x:", human.x, " y:", human.y)

        human.rushx = nil
        human.rushy = nil
        return		
    end

    human.rushx = nil
    human.rushy = nil
    human.path_client_x = m.x
    human.path_client_y = m.y
    human.path_time = Timer.now
end

function CG_JUMP(human, msg)
    if human.hide then
        return
    end

    if human.scene_id == nil then
        return
    end

    if human.db.hp < 1 then
        return 
    end
    if human.db.isMount ~= 1 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_LUOMA)
        return
    end
    if Cmds.passive.awake(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_AWAKE)
        return
    end
    if Cmds.passive.giddy(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_GIDDY)
        return
    end
    if Cmds.passive.noMove(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_NOMOVE)
        return    
    end
    if Cmds.passive.noSkill(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_NOSKILL)
        return    
    end


    local canJump
    local canThrough
    canJump, canThrough = Map.canSpaceJump(human)
    if not canJump then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_AREA_CANT)
        return
    end

    if human.db.jumpValue < BaseAttrInitValue.jump_cost then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.JUMP_SPACE_CANT)
        return
    end



    local tx = msg.mousex
    local ty = msg.mousey
    local dx = tx- human.x
    local dy = ty - human.y
    local curLen = math.sqrt(dx*dx + dy*dy)
    local len = 500
    tx, ty = human.x + dx * len / curLen, human.y + dy * len / curLen
    SceneEvent.setBan(human.scene_id)
    if not canThrough then
        tx, ty = Map.through(human.scene_id, human.x, human.y, tx, ty)      
    else
        tx, ty = Map.through_jump(human.scene_id, human.x, human.y, tx, ty)
    end
    dx = tx - human.x
    dy = ty - human.y
    curLen = math.sqrt(dx*dx + dy*dy)
    if curLen > 600 then
        return
    end
    human.db.jumpValue = human.db.jumpValue - BaseAttrInitValue.jump_cost
    Status.onJump(human)
    Obj.jump(human, tx, ty,Msg.gc.GC_SPACE_JUMP)      

    human.attr[RoleDefine.JUMP_VALUE] = human.db.jumpValue
    ObjHuman.sendAttr(human, RoleDefine.JUMP_VALUE)

    local pet = Obj.getObj(human.fight_pet_id, human.fight_pet_uid)
    if pet and (not pet.wake_up_time or pet.wake_up_time <= Timer.now ) then
        pet.wake_up_time = Timer.now + 500
    end
end

function CG_LINE1_FN_ICON_LIST(human)
    RoleUIControl.sendLine1FnIcon(human)
    RoleUIControl.sendPreknow(human)
end


function CG_SERVER_TIME(human)
    local msgRet = Msg.gc.GC_SERVDR_TIME
    msgRet.server_time = os.time()
    Msg.send(msgRet,human.fd)
end

function WL_ZHENG_DIAN(fd, msg)
    Timer.onZhengDian(msg.hour)
end

--觉醒
function CG_AWAKE(human)
    if human.db.awakeVal  < 1000 then
        return
    end
    if human.scene_id == nil then
        return
    end
    if human.hide then
        return
    end
    if CopyL.isCopyBySceneID(human.scene_id) == CopyDefine.COPY_DATI_ID then  
        return
    end

    if human.db.hp < 1 then
        return 
    end
    if Cmds.passive.awake(human) then
        return
    end
    if human.db.isMount == 0 then    
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.AWAKE_ERR_LUOMA)
        return
    end

    if Cmds.passive.giddy(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.AWAKE_ERR_GIDDY)
        return
    end
    if Cmds.passive.noSkill(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.AWAKE_ERR_NOSKILL)
        return    
    end

    if Copy.isCopyBySceneID(human.scene_id) == BeginStory.BEGIN_STORY_COPY_NEW_BORN then
        ObjHuman.clearAwake(human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_ATK_BUFF_ID, human, human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_HP_BUFF_ID, human, human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_SPEED_BUFF_ID, human,human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_HUIXIN_BUFF_ID, human,human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_ZUOYUE_BUFF_ID, human,human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_BAOJI_BUFF_ID, human,human)
        Buffer.add(BeginStory.BEGIN_STORY_BORN_ZHAOYUN_AWAKE_BUFF_ID, human,human)
        Status.sendViewStatus(human,true)
        return
    end
    -- 加buff
    if human.db.shenbingLen == 0 then
        return
    end
    ObjHuman.clearAwake(human)
    local sb = sbConfig[human.db.putonShenbing]
    if not sb then
        assert()
    end

    --切换到觉醒状态
    for k,v in pairs(sb.awakeSkillBuffID) do
        Buffer.add(v, human, human)
    end
    Status.sendViewStatus(human,true)
end

function CG_GET_MONSTERCORPSE_REWARD(human, msg)
    local msgErr = Msg.gc.GC_BROADCAST
    msgErr.type = Lang.MOUSE
    local nType = msg.type
    local nObjID = msg.monster_id
    local nObj = Obj.getObj(nObjID)
    if not nObj or nObj.obj_type ~= Obj.TYPE_MONSTER then
        return
    end
    local nSceneID = nObj.scene_id
    if human.scene_id == nSceneID and ObjMonster.MONSTERCORPSE_LIST[nSceneID] and ObjMonster.MONSTERCORPSE_LIST[nSceneID][nObjID] then
        local nMonsterConfig = ExcelMonsterConf.monster[nObj.monster_id]
        if nMonsterConfig.canCorpseReward == 1 then
            local nMaxRewardTimes = #nMonsterConfig.corpseRewardData
            if nMaxRewardTimes > 0 then
                ObjMonster.MONSTERCORPSE_LIST[nObj.scene_id][nObjID][human._id] = ObjMonster.MONSTERCORPSE_LIST[nObj.scene_id][nObjID][human._id] or {curtimes = 0}
                local nMonsterCorpseData = ObjMonster.MONSTERCORPSE_LIST[nObj.scene_id][nObjID][human._id]
                if nMonsterCorpseData.curtimes >= nMaxRewardTimes then
                    msgErr.msg = "已挖达到最大次数"
                    Msg.send(msgErr, human.fd)
                    return
                end

                local nCorpseRewardData = nMonsterConfig.corpseRewardData[nMonsterCorpseData.curtimes + 1]
                if nCorpseRewardData then
                    if nCorpseRewardData[1] == 1 then
                        if ObjHuman.getYinliang(human) < nCorpseRewardData[2] then
                            msgErr.msg = "银两不足"
                            Msg.send(msgErr, human.fd)
                            return
                        end
                    elseif nCorpseRewardData[1] == 2 then
                        if ObjHuman.getYuanbao(human) < nCorpseRewardData[2] then
                            msgErr.msg = "元宝不足"
                            Msg.send(msgErr, human.fd)
                            return
                        end
                    elseif nCorpseRewardData[1] == 3 then
                        if ObjHuman.getBindYuanbao(human) < nCorpseRewardData[2] then
                            msgErr.msg = "绑定元宝不足"
                            Msg.send(msgErr, human.fd)
                            return
                        end
                    end

                    local nCanAdd = false
                    for nRewardIndex, nRewardData in ipairs(nCorpseRewardData[3]) do
                        nCanAdd = BagLogic.checkCanAddItem(human, nRewardData[1], nRewardData[4] or true, nRewardData[2])
                        if nCanAdd ~= true then
                            break
                        end
                    end

                    if not nCanAdd then
                        msgErr.msg = "背包空间不足"
                        Msg.send(msgErr, human.fd)
                        return
                    end

                    nMonsterCorpseData.curtimes = nMonsterCorpseData.curtimes + 1
                    if nCorpseRewardData[1] == 1 then
                        ObjHuman.decYinliang(human, nCorpseRewardData[2], "monsterCorpse")
                    elseif nCorpseRewardData[1] == 2 then
                        ObjHuman.decYuanbao(human, nCorpseRewardData[2], "monsterCorpse")
                    elseif nCorpseRewardData[1] == 3 then
                        ObjHuman.decBindYuanbao(human, nCorpseRewardData[2], "monsterCorpse")
                    end

                    for nRewardIndex, nRewardData in ipairs(nCorpseRewardData[3]) do
                        if ItemDefine.isEquip(nRewardData[1]) then
                            local nGridData = Grid.create(nil, nRewardData[1], nRewardData[2], nRewardData[4] or true)
                            BagLogic.addItemByGrid(human, nil, nGridData, "monsterCorpse")
                        else
                            BagLogic.addItem(human, nRewardData[1], nRewardData[4] or true, nRewardData[2], "monsterCorpse")
                        end
                    end

                    local nSendMsg = Msg.gc.GC_GET_MONSTERCORPSE_REWARD
                    nSendMsg.type = 0
                    nSendMsg.monster_id = msg.monster_id
                    nSendMsg.curtimes = nMonsterCorpseData.curtimes
                    Msg.send(nSendMsg, human.fd)
                end
            end
        end
    end
end

